package cn.jcet.mapreduce.wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/2 0:11
 */
public class WordcountDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        args = new String[]{"e:/code/input","e:/code/output2"};

        Configuration config = new Configuration();
        //1、获取配置Job对象
        Job job = Job.getInstance(config);

        //2、获取jar的存储位置,通过反射加载类位置
        job.setJarByClass(WordcountDriver.class);

        //3、关联Map和Reduce类位置
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReduce.class);

        // 关联合并
        job.setCombinerClass(WordcountCombiner.class);

        //4、设置map的输出key value数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //5、设置最终输出的key和value数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //6、设置输入和输出位置 参数设置
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        //7、提交任务
        boolean result = job.waitForCompletion(true);
        // 打印信息
        System.out.println(result ? 0 : 1);
    }
}
